/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.monVehicule.dao.ajouter;

import fr.monVehicule.entite.Vehicule;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author David
 */
public class VehiculeDAO extends DAO<Vehicule> {

    public VehiculeDAO() {
        super(Vehicule.class);
    }

    public Vehicule saveVehicule(Vehicule vehicule) {
       
        session.getTransaction().begin();
        session.save(vehicule);
        session.getTransaction().commit();
        session.close();
        return vehicule;
    }

    public List<Vehicule> loadVehicules() {
        
        List<Vehicule> list = null;
        Criteria vehiculeCriteria = createCriteria();
        vehiculeCriteria.createAlias("typeVehicule", "typeVehicule");
        vehiculeCriteria.createAlias("marque", "marque");
        vehiculeCriteria.createAlias("modele", "modele");
        list = vehiculeCriteria.list();
        return list;
    }

    public Vehicule findVehiculeById(Integer idVehicule) {
       
        List<Vehicule> list = null;
        Criteria vehiculeCriteria = createCriteria();
        vehiculeCriteria.add(Restrictions.eq("id", idVehicule));
        vehiculeCriteria.createAlias("typeVehicule", "typeVehicule");
        vehiculeCriteria.createAlias("marque", "marque");
        vehiculeCriteria.createAlias("modele", "modele");
        list = vehiculeCriteria.list();
        return list.get(0);
    }
}
